package com.imooc.trading_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.imooc.trading_system.enums.BizCodeEnum;
import com.imooc.trading_system.exception.BizException;
import com.imooc.trading_system.model.UserDO;
import com.imooc.trading_system.model.WalletDO;
import com.imooc.trading_system.mapper.WalletMapper;
import com.imooc.trading_system.service.UserService;
import com.imooc.trading_system.service.WalletService;
import com.imooc.trading_system.utils.JsonData;
import com.imooc.trading_system.vo.wallet.GetWalletInfoRespVO;
import com.imooc.trading_system.vo.wallet.TopUpReqVO;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Feng zhaoxi
 * @since 2022-05-03
 */
@Service
public class WalletServiceImpl implements WalletService {
    @Autowired
    private UserService userService;
    @Resource
    private WalletMapper walletMapper;

    /**
     * 获取钱包信息
     *
     * @return
     */
    @Override
    public JsonData getWalletInfo() {
        UserDO userDO = userService.getLoginUser();
        WalletDO walletDO = walletMapper.selectOne(new QueryWrapper<WalletDO>()
                .eq("user_id", userDO.getId()));
        if (walletDO == null) {
            return JsonData.buildResult(BizCodeEnum.DO_NOT_OPEN_WALLET);
        }
        GetWalletInfoRespVO getWalletInfoRespVO = new GetWalletInfoRespVO();
        BeanUtils.copyProperties(walletDO, getWalletInfoRespVO);
        return JsonData.buildSuccess(getWalletInfoRespVO);
    }

    /**
     * 充值
     *
     * @param topUpReqVO
     * @return
     */
    @Override
    public JsonData topUp(TopUpReqVO topUpReqVO) {
        UserDO userDO = userService.getLoginUser();
        WalletDO walletDO = walletMapper.selectOne(new QueryWrapper<WalletDO>()
                .eq("user_id", userDO.getId()));
        BigDecimal balance = walletDO.getBalance();
        UpdateWrapper<WalletDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userDO.getId());
        walletDO.setBalance(balance.add(topUpReqVO.getTopUpValue()));
        walletMapper.update(walletDO, updateWrapper);
        return JsonData.buildSuccess();
    }

    /**
     * 扣减余额
     *
     * @param deductions
     * @return
     */
    @Override
    public int deductions(BigDecimal deductions) {
        UserDO userDO = userService.getLoginUser();
        WalletDO walletDO = walletMapper.selectOne(new QueryWrapper<WalletDO>()
                .eq("user_id", userDO.getId()));
        BigDecimal balance = walletDO.getBalance();
        if (balance.compareTo(deductions) < 0) {
            throw new BizException(BizCodeEnum.BALANCE_NOT_ENOUGH);
        }
        UpdateWrapper<WalletDO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userDO.getId());
        walletDO.setBalance(balance.subtract(deductions));
        int rows = walletMapper.update(walletDO, updateWrapper);
        if (rows != 1) {
            throw new BizException(BizCodeEnum.DEDUCTIONS_FAILED);
        }
        return rows;
    }

    /**
     * 获取余额
     *
     * @return
     */
    @Override
    public BigDecimal getWalletBalance() {
        UserDO userDO = userService.getLoginUser();
        WalletDO walletDO = walletMapper.selectOne(new QueryWrapper<WalletDO>()
                .eq("user_id", userDO.getId()));
        return walletDO.getBalance();
    }

    /**
     * 开启钱包
     *
     * @return
     */
    @Override
    public JsonData OpenWallet() {
        UserDO userDO = userService.getLoginUser();
        WalletDO walletDO = walletMapper.selectOne(new QueryWrapper<WalletDO>()
                .eq("user_id", userDO.getId()));
        if (walletDO != null) {
            return JsonData.buildResult(BizCodeEnum.HAD_OPEN_WALLET);
        }
        WalletDO newWalletDO = new WalletDO();
        newWalletDO.setUserId(userDO.getId());
        newWalletDO.setBalance(new BigDecimal(0));
        walletMapper.insert(newWalletDO);
        return JsonData.buildSuccess();
    }

    /**
     * 关闭钱包
     *
     * @return
     */
    @Override
    public JsonData closeWallet() {
        UserDO userDO = userService.getLoginUser();
        WalletDO walletDO = walletMapper.selectOne(new QueryWrapper<WalletDO>()
                .eq("user_id", userDO.getId()));
        if (walletDO == null) {
            return JsonData.buildResult(BizCodeEnum.DO_NOT_OPEN_WALLET);
        }
        if (walletDO.getBalance().equals(BigDecimal.ZERO)) {
            walletMapper.deleteById(walletDO.getId());
            return JsonData.buildSuccess();
        } else {
            return JsonData.buildResult(BizCodeEnum.CLOSE_WALLET_FAILED);
        }
    }
}
